package 算法基础模板.chapter_02;


import java.util.Scanner;

/**
 * 单调栈
 * <blockquote><pre>{@code
 *
 * 常见模型：找出每个数左边离它最近的比它大/小的数
 * int tt = 0;
 * for (int i = 1; i <= n; i ++ )
 * {
 *     while (tt && check(stk[tt], i)) tt -- ;
 *     stk[ ++ tt] = i;
 * }
 * }</pre></blockquote>
 *
 * @author Summerday
 */
public class MonotonicStack {

    static int n;
    static int[] stk;
    static int tt;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        stk = new int[n];
        for (int i = 0; i < n; i++) {
            int x = sc.nextInt();
            while (tt > 0 && x <= stk[tt]) {
                tt--;
            }
            if (tt < 0) System.out.println("-1");
            else System.out.println(stk[tt]);
            tt++;
            stk[tt] = x;
        }

    }
}
